package org.wmq.travelagency02.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wmq.travelagency02.mapper.TourGroupTravellerMapper;
import org.wmq.travelagency02.model.Entity.TourGroup;
import org.wmq.travelagency02.model.Entity.TourGroupTraveller;
import org.wmq.travelagency02.model.Entity.Traveller;
import org.wmq.travelagency02.service.TourGroupService;
import org.wmq.travelagency02.service.TourGroupTravellerService;
import org.wmq.travelagency02.service.TravellerService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TourGroupTravellerServiceImpl extends ServiceImpl<TourGroupTravellerMapper, TourGroupTraveller> implements TourGroupTravellerService {
    @Lazy
    @Autowired
    private TravellerService travellerService;
    @Lazy
    @Autowired
    private TourGroupService tourGroupService;

    @Override
    public List<Traveller> getTravellersByTourGroupId(Long tourGroupId) {
        List<TourGroupTraveller> list = this.lambdaQuery().eq(TourGroupTraveller::getTourGroupId, tourGroupId).
                select(TourGroupTraveller::getTravellerId).list();
        if (list == null || list.size() == 0){
            return null;
        }
        List<Traveller> result = new ArrayList<>();
        for (TourGroupTraveller tourGroupTraveller : list) {
            Traveller traveller = this.travellerService.getById(tourGroupTraveller.getTravellerId());
            result.add(traveller);
        }
        return result;
    }

    @Override
    public boolean addTravellerToTourGroup(Long travellerId, Long tourGroupId) {
        Long tourGroupCurrentSize = this.getTourGroupCurrentSizeByTourGroupId(tourGroupId);
        TourGroup tourGroup = this.tourGroupService.lambdaQuery().eq(TourGroup::getId, tourGroupId)
                .select(TourGroup::getMaxSize).one();
        Byte tourGroupMaxSize = tourGroup.getMaxSize();
        if ((long)tourGroupCurrentSize == (long)tourGroupMaxSize){
            return false;
        }
        TourGroupTraveller tourGroupTraveller = new TourGroupTraveller(tourGroupId, travellerId);
        tourGroupTraveller.setCreateTime(LocalDateTime.now());
        tourGroupTraveller.setUpdateTime(LocalDateTime.now());
        return this.save(tourGroupTraveller);
    }

    @Override
    public Long getTourGroupCurrentSizeByTourGroupId(Long tourGroupId) {
        return this.lambdaQuery().eq(TourGroupTraveller::getTourGroupId, tourGroupId)
                .count();
    }

    @Override
    public boolean removeByTravellerIdAndTourGroupId(Long travellerId, Long tourGroupId) {
        return this.lambdaUpdate().eq(TourGroupTraveller::getTravellerId, travellerId)
                .eq(TourGroupTraveller::getTourGroupId, tourGroupId).remove();
    }


}
